home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / alphabuf.c < prev    next >
C/C++ Source or Header  |  1999-02-04  |  6KB  |  265 lines

  1. /* $Id: alphabuf.c,v 3.2 1998/03/28 03:57:13 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: alphabuf.c,v $
  26.  * Revision 3.2  1998/03/28 03:57:13  brianp
  27.  * added CONST macro to fix IRIX compilation problems
  28.  *
  29.  * Revision 3.1  1998/03/27 04:40:28  brianp
  30.  * added a few const keywords
  31.  *
  32.  * Revision 3.0  1998/01/31 20:45:34  brianp
  33.  * initial rev
  34.  *
  35.  */
  36.  
  37.  
  38.  
  39. /*
  40.  * Software alpha planes.  Many frame buffers don't have alpha bits so
  41.  * we simulate them in software.
  42.  */
  43.  
  44.  
  45. #ifdef PC_HEADER
  46. #include "all.h"
  47. #else
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "alphabuf.h"
  51. #include "context.h"
  52. #include "macros.h"
  53. #include "types.h"
  54. #endif
  55.  
  56.  
  57.  
  58. #define ALPHA_ADDR(X,Y)  (ctx->Buffer->Alpha + (Y) * ctx->Buffer->Width + (X))
  59.  
  60.  
  61.  
  62. /*
  63.  * Allocate a new front and back alpha buffer.
  64.  */
  65. void gl_alloc_alpha_buffers( GLcontext* ctx )
  66. {
  67.    GLint bytes = ctx->Buffer->Width * ctx->Buffer->Height * sizeof(GLubyte);
  68.  
  69.    if (ctx->Visual->FrontAlphaEnabled) {
  70.       if (ctx->Buffer->FrontAlpha) {
  71.      free( ctx->Buffer->FrontAlpha );
  72.       }
  73.       ctx->Buffer->FrontAlpha = (GLubyte *) malloc( bytes );
  74.       if (!ctx->Buffer->FrontAlpha) {
  75.      /* out of memory */
  76.      gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate front alpha buffer" );
  77.       }
  78.    }
  79.    if (ctx->Visual->BackAlphaEnabled) {
  80.       if (ctx->Buffer->BackAlpha) {
  81.      free( ctx->Buffer->BackAlpha );
  82.       }
  83.       ctx->Buffer->BackAlpha = (GLubyte *) malloc( bytes );
  84.       if (!ctx->Buffer->BackAlpha) {
  85.      /* out of memory */
  86.      gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate back alpha buffer" );
  87.       }
  88.    }
  89.    if (ctx->Color.DrawBuffer==GL_FRONT) {
  90.       ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha;
  91.    }
  92.    if (ctx->Color.DrawBuffer==GL_BACK) {
  93.       ctx->Buffer->Alpha = ctx->Buffer->BackAlpha;
  94.    }
  95. }
  96.  
  97.  
  98.  
  99. /*
  100.  * Clear the front and/or back alpha planes.
  101.  */
  102. void gl_clear_alpha_buffers( GLcontext* ctx )
  103. {
  104.    GLint buffer;
  105.  
  106.    /* Loop over front and back buffers */
  107.    for (buffer=0;buffer<2;buffer++) {
  108.  
  109.       /* Get pointer to front or back buffer */
  110.       GLubyte *abuffer = NULL;
  111.       if (buffer==0
  112.       && (   ctx->Color.DrawBuffer==GL_FRONT
  113.           || ctx->Color.DrawBuffer==GL_FRONT_AND_BACK)
  114.       && ctx->Visual->FrontAlphaEnabled && ctx->Buffer->FrontAlpha) {
  115.      abuffer = ctx->Buffer->FrontAlpha;
  116.       }
  117.       else if (buffer==1
  118.            && (   ctx->Color.DrawBuffer==GL_BACK
  119.            || ctx->Color.DrawBuffer==GL_FRONT_AND_BACK)
  120.            && ctx->Visual->BackAlphaEnabled && ctx->Buffer->BackAlpha) {
  121.      abuffer = ctx->Buffer->BackAlpha;
  122.       }
  123.  
  124.       /* Clear the alpha buffer */
  125.       if (abuffer) {
  126.      GLubyte aclear = (GLint) (ctx->Color.ClearColor[3] * 255.0F);
  127.      if (ctx->Scissor.Enabled) {
  128.         /* clear scissor region */
  129.         GLint i, j;
  130.         for (j=0;j<ctx->Scissor.Height;j++) {
  131.            GLubyte *aptr = ALPHA_ADDR(ctx->Buffer->Xmin,
  132.                       ctx->Buffer->Ymin+j);
  133.            for (i=0;i<ctx->Scissor.Width;i++) {
  134.           *aptr++ = aclear;
  135.            }
  136.         }
  137.      }
  138.      else {
  139.         /* clear whole buffer */
  140.         MEMSET( abuffer, aclear, ctx->Buffer->Width*ctx->Buffer->Height );
  141.      }
  142.       }
  143.    }
  144. }
  145.  
  146.  
  147.  
  148. void gl_write_alpha_span( GLcontext* ctx, GLuint n, GLint x, GLint y,
  149.               CONST GLubyte rgba[][4], const GLubyte mask[] )
  150. {
  151.    GLubyte *aptr = ALPHA_ADDR( x, y );
  152.    GLuint i;
  153.  
  154.    if (mask) {
  155.       for (i=0;i<n;i++) {
  156.      if (mask[i]) {
  157.         *aptr = rgba[i][ACOMP];
  158.      }
  159.      aptr++;
  160.       }
  161.    }
  162.    else {
  163.       for (i=0;i<n;i++) {
  164.      *aptr++ = rgba[i][ACOMP];
  165.       }
  166.    }
  167. }
  168.  
  169.  
  170. void gl_write_mono_alpha_span( GLcontext* ctx, GLuint n, GLint x, GLint y,
  171.                    GLubyte alpha, const GLubyte mask[] )
  172. {
  173.    GLubyte *aptr = ALPHA_ADDR( x, y );
  174.    GLuint i;
  175.  
  176.    if (mask) {
  177.       for (i=0;i<n;i++) {
  178.      if (mask[i]) {
  179.         *aptr = alpha;
  180.      }
  181.      aptr++;
  182.       }
  183.    }
  184.    else {
  185.       for (i=0;i<n;i++) {
  186.      *aptr++ = alpha;
  187.       }
  188.    }
  189. }
  190.  
  191.  
  192. void gl_write_alpha_pixels( GLcontext* ctx,
  193.                 GLuint n, const GLint x[], const GLint y[],
  194.                 CONST GLubyte rgba[][4], const GLubyte mask[] )
  195. {
  196.    GLuint i;
  197.  
  198.    if (mask) {
  199.       for (i=0;i<n;i++) {
  200.      if (mask[i]) {
  201.         GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  202.         *aptr = rgba[i][ACOMP];
  203.      }
  204.       }
  205.    }
  206.    else {
  207.       for (i=0;i<n;i++) {
  208.      GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  209.      *aptr = rgba[i][ACOMP];
  210.       }
  211.    }
  212. }
  213.  
  214.  
  215. void gl_write_mono_alpha_pixels( GLcontext* ctx,
  216.                  GLuint n, const GLint x[], const GLint y[],
  217.                  GLubyte alpha, const GLubyte mask[] )
  218. {
  219.    GLuint i;
  220.  
  221.    if (mask) {
  222.       for (i=0;i<n;i++) {
  223.      if (mask[i]) {
  224.         GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  225.         *aptr = alpha;
  226.      }
  227.       }
  228.    }
  229.    else {
  230.       for (i=0;i<n;i++) {
  231.      GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  232.      *aptr = alpha;
  233.       }
  234.    }
  235. }
  236.  
  237.  
  238.  
  239. void gl_read_alpha_span( GLcontext *ctx,
  240.              GLuint n, GLint x, GLint y, GLubyte rgba[][4] )
  241. {
  242.    GLubyte *aptr = ALPHA_ADDR( x, y );
  243.    GLuint i;
  244.    for (i=0;i<n;i++) {
  245.       rgba[i][ACOMP] = *aptr++;
  246.    }
  247. }
  248.  
  249.  
  250. void gl_read_alpha_pixels( GLcontext *ctx,
  251.                GLuint n, const GLint x[], const GLint y[],
  252.                GLubyte rgba[][4], const GLubyte mask[] )
  253. {
  254.    GLuint i;
  255.    for (i=0;i<n;i++) {
  256.       if (mask[i]) {
  257.      GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  258.      rgba[i][ACOMP] = *aptr;
  259.       }
  260.    }
  261. }
  262.  
  263.  
  264.  
  265.